package com.qfedu.bookstores.dao.impl;

import com.qfedu.bookstores.dao.BaseDao;
import com.qfedu.bookstores.dao.IBookDao;
import com.qfedu.bookstores.entity.Book;

import java.util.List;

public class IBookDaoImpl extends BaseDao implements IBookDao {
    @Override
    public int addBook(Book book) {
       String sql = "insert into t_book(name,author,price,sales,stock,img_path)values(?,?,?,?,?,?)";
       return update(sql, book.getName(),book.getAuthor(),book.getPrice()
               ,book.getSales(),book.getStock(),book.getImg_path());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id = ?";
        return update(sql, id);
    }

    @Override
    public int updateBook(Book book) {
        String  sql = "update t_book set name=?,author=?,price=?,sales=?,stock=?,img_path=? where id=?";
        return update(sql, book.getName(),book.getAuthor(),book.getPrice()
                ,book.getSales(),book.getStock(),book.getImg_path(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql ="select id,name,author,price,sales,stock,img_path from t_book where id = ?";
        return queryForOne(Book.class, sql,id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql ="select id,name,author,price,sales,stock,img_path from t_book";
        return queryForList(Book.class, sql);
    }
}
